package com.wwwarehouse.taskcenter.fragment.job_point.step2.child;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.PointResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointStateEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_WAREHOUSE_UKID;

/**
 * Created by Jackie on 2017/10/27
 * 子作业点位置维度  选择添加方式  按工作点添加
 */

public class ChooseAddWayChildPositionByPointFragment extends CommonBasePagerFragment {
    private String mWarehouseUkid;

    private ArrayList<PointResponseBean> mPointDetailsList;

    private static final int REQUEST_POINT_INFO = 0;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        ((BaseCardDeskActivity) mActivity).showFilterBt();
        ((BaseCardDeskActivity) mActivity).showSortBt();

        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        mPointDetailsList = new ArrayList<>();
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mWarehouseUkid = bundle.getString(KEY_WAREHOUSE_UKID);
        }
    }

    private void initEvent() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDownPull() {
        if (TaskCenterCommon.getInstance().isDetails()) {

        } else {
            super.onDownPull();

            ((BaseCardDeskActivity) mActivity).hideFilterBt();
            ((BaseCardDeskActivity) mActivity).hideSortBt();
        }
    }

    @Override
    public void textNoRealChanged(String text) {
        if (TaskCenterCommon.getInstance().isDetails()) {

        } else {
            if (peekFragment() instanceof ChooseAddWayChildPositionByPointFragment) {
                ArrayList<PointResponseBean> pointDetailsList = new ArrayList<>();

                for (PointResponseBean pointDetailsBean : mPointDetailsList) {
                    if (pointDetailsBean.getItemName().contains(text)) {
                        pointDetailsList.add(pointDetailsBean);
                    }
                }

                try {
                    Bundle bundle = new Bundle();
                    setData(pointDetailsList.size(), 10, new ChooseAddWayChildPositionByAreaDetailsFragment().getClass().getName(), bundle, pointDetailsList);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (pointDetailsList.size() > 0) {
                    initConfirmBtn();
                }
            }
        }
    }

    @Override
    public void cancelClick() {
        if (TaskCenterCommon.getInstance().isDetails()) {

        } else {
            if (peekFragment() instanceof ChooseAddWayChildPositionByPointFragment) {
                getPointInfo();
            }
        }
    }

    @Override
    public void onRefreshListener() {
        if (TaskCenterCommon.getInstance().isDetails()) {

        } else {
            getPointInfo();
        }
    }

    @Override
    public void onLoadSuccess(String data, int what) {
        switch (what) {
            case REQUEST_POINT_INFO:
                mPointDetailsList = (ArrayList<PointResponseBean>) JSON.parseArray(data, PointResponseBean.class);

                try {
                    Bundle bundle = new Bundle();
                    setData(mPointDetailsList.size(), 10, new ChooseAddWayChildPositionByPointDetailsFragment().getClass().getName(), bundle, mPointDetailsList);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (mPointDetailsList.size() > 0) {
                    initConfirmBtn();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onLoadError(String s) {
        toast(s);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseAddWayChildPositionByPointFragment) {
            if (TaskCenterCommon.getInstance().isStart()) {
                mActivity.setTitle(R.string.choose_start_value);
            } else {
                mActivity.setTitle(R.string.choose_end_value);
            }
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        if (TaskCenterCommon.getInstance().isDetails()) {

        } else {
            getPointInfo();
        }
    }

    public void onEventMainThread(JobPointStateEvent event) {
        if (peekFragment() instanceof ChooseAddWayChildPositionByPointFragment) {
            if (TaskCenterCommon.getInstance().isDetails()) {

            } else {
                for (PointResponseBean pointDetailsBean : mPointDetailsList) {
                    if (pointDetailsBean.isSelect()) {
                        mConfirmBtn.setEnabled(true);

                        return;
                    }
                }

                mConfirmBtn.setEnabled(false);
            }
        }
    }

    private void getPointInfo() {
        Map map = new HashMap();

        map.put("warehouseUkid", mWarehouseUkid);

        loadData(TaskCenterConstant.URL_GET_POINT_INFO, map, REQUEST_POINT_INFO);

    }

    private void initConfirmBtn() {
        showConfirmButton();

        if (TaskCenterCommon.getInstance().isDetails()) {

        } else {
            mConfirmBtn.setEnabled(false);
        }

        setConfirmBtnText(mActivity.getString(R.string.confirm));

        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TaskCenterCommon.getInstance().isDetails()) {
                    popFragment();
                } else {
                    popFragment();
                    popFragment();
                }

                buildPointMap(mPointDetailsList);

                //反向传值
                Intent intent = new Intent("ChooseAddWayChildPositionByPoint");
                mActivity.sendBroadcast(intent);
            }
        });
    }

    private void buildPointMap(List<PointResponseBean> pointDetailsList) {
        Map map = new HashMap();
        List<Map<String, String>> list = new ArrayList<>();

        for (PointResponseBean pointDetailBean : pointDetailsList) {
            if (pointDetailBean.isSelect()) {
                Map<String, String> listMap = new HashMap<>();

                listMap.put("key", pointDetailBean.getLocationUkid());
                listMap.put("value", pointDetailBean.getItemName());

                list.add(listMap);
            }
        }

        map.put("list", list);
        map.put("type", "Position_POINT");

        if (TaskCenterCommon.getInstance().isStart()) {
            TaskCenterCommon.getInstance().setStartMap(map);
        } else {
            TaskCenterCommon.getInstance().setEndMap(map);
        }
    }
}
